home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / macros / latex209 / contrib / wright / wright.sty < prev    next >
Text File  |  1993-11-07  |  2KB  |  75 lines

  1. %%%%%%%%%%%%%%%%%%%%%%%cut here%%%%%%%%%%%%%%%%%%%%%%%
  2. %% wright.sty--example of how to change LaTeX's \newcommand (and \renewcommand)
  3. %%   to define a command with an optional argument.
  4. %% Usage:
  5. %%  \newcommand and \renewcommand are backward compatible.
  6. %%  To define a command with an optional argument, these commands will take 
  7. %%  a second optional argument:
  8. %%
  9. %%  \newcommand\kilroy[1][\blech]{\foo#1\bar#2\baz}%
  10. %%
  11. %%  will define \kilroy to have an optional argument with default value of
  12. %%  \blech and expansion \foo#1\bar#2\baz, where the optional argument is #1.
  13. %%  NB: the optional argument is the first parameter. You must bear this in
  14. %%  mind when using the \newcommand macro.
  15. %%  NB: If you wish to define a command that takes an optional argument, but no
  16. %%    required arguments, usage is, eg:
  17. %%
  18. %%  \newcommand\kilroy[0][\blech]{\foo#1\bar#2\baz}%
  19. %%
  20. %% Implementation:
  21. %%  modified commands:
  22. %%   \newcommand
  23. %%   \renewcommand
  24. %%
  25. %%  new commands:
  26. %%   \@newcommand
  27. %%   \@@newcommand
  28. %%   \@@@newcommand
  29. %%   \@ifredefinable
  30. %%   \@@ifredefinable
  31. %%   \@commanddef
  32. %%   \@ldef
  33. %%   \@hashchar
  34. %%   \@macparm
  35. %%
  36. %%  commands that change their meaning when these macros are executed:
  37. %%   \@@ifredefinable
  38. %%   \@macparm
  39. %%   
  40. \def\newcommand{\let\@@ifdefinable\@ifdefinable\@newcommand}%
  41. \def\renewcommand{\let\@@ifdefinable\@ifredefinable\@newcommand}%
  42.  
  43. \def\@newcommand#1{\@ifnextchar[{\@@newcommand#1}{\@@newcommand#1[\z@]}}%
  44. \def\@@newcommand#1[#2]{\@ifnextchar[{\@@@newcommand#1[#2]}{\@@@newcommand#1[#2][]}}%
  45. \long\def\@@@newcommand#1[#2][#3]#4{\@@ifdefinable#1{\@commanddef#1[#2][#3]{#4}}}%
  46.  
  47. \def\@ifredefinable#1#2{%
  48.  \edef\@tempa{\expandafter\@cdr\string#1\@nil}%
  49.  \@ifundefined{\@tempa}{\@latexerr{\string#1\space undefined}\@ehc}{}%
  50.  \let#1\undefined
  51.  \@ifdefinable#1{#2}%
  52.  }%
  53.  
  54. \long\def\@commanddef#1[#2][#3]#4{%
  55.  \let#1\relax\@tempcnta#2\def\@tempb{#3}\let\@macparm\relax
  56.  \ifx\@tempb\@empty
  57.   \@tempcntb\@ne
  58.   \edef\@tempa{#1}%
  59.   \else
  60.   \@tempcntb\tw@
  61.   \edef\@tempa{\csname\string#1\endcsname[\@macparm1]}%
  62.   \def#1{%
  63.    \@ifnextchar[{\csname\string#1\endcsname}{\csname\string#1\endcsname[#3]}%
  64.    }%
  65.   \fi
  66.  \@whilenum\@tempcnta>\z@\do{%
  67.   \edef\@tempa{\@tempa\@macparm\the\@tempcntb}%
  68.   \advance\@tempcntb\@ne\advance\@tempcnta\m@ne
  69.   }%
  70.  \let\@macparm\@hashchar\expandafter\@ldef\@tempa{#4}%
  71.  }%
  72. \catcode`\?=12\relax
  73. \def\@ldef{\long\def}%
  74. \let\@hashchar#% the macro-parameter catcode
  75.